package com.sufen.seckill.study.LeetCode;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * @Author sufen
 * @Date 2021/11/4 16:05
 * @Version 1.0
 * 功能描述：
 */
public class Offer_04_二维数组中的查找 {
    public static void main(String[] args) throws Exception {

        //反射通过名字获取Class
        Class clazz= Class.forName("com.sufen.seckill.study.LeetCode.Offer_03_数组中重复的数字");
        //获取构造器Constructor，生成一个对象
        Constructor constructor=clazz.getConstructor(null);
        Object a= constructor.newInstance();
        //获取private的方法
        Method method=clazz.getDeclaredMethod("findRepeatNumber",int[].class);
        method.setAccessible(true);
        int[] input={1,2,3,4,5,6,3,7,8};
        System.out.println(method.invoke(a,input));
//        int[][] inputMatrix={{1,3,5},{2,4,6},{3,5,7}};
//        int target=8;
//        System.out.println(findNumberIn2DArray(inputMatrix,target));
    }
    public static boolean findNumberIn2DArray(int[][] matrix, int target) {
        int length_hang=matrix.length;
        if(length_hang==0){
            return false;
        }
        int length_lie=matrix[0].length;
        int start_hang=length_hang-1;
        int start_lie=0;

        while(start_hang>=0&&start_lie<length_lie){
            if(matrix[start_hang][start_lie]==target){
                return true;
            }else if(matrix[start_hang][start_lie]>target){
                start_hang--;
            }else{
                start_lie++;
            }
        }

        return false;
    }
}
